package com.itheima.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.QuestionsReport;
import com.itheima.dubbo.mapper.QuestionsReportMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/3/1 20:57
 */
@Service
public class QuestionsReportApiImpl implements QuestionsReportApi {

    @Autowired
    private QuestionsReportMapper reportMapper;

    @Override
    public QuestionsReport findByUserId(Long userId) {
        QueryWrapper<QuestionsReport> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return reportMapper.selectOne(wrapper);
    }

    @Override
    public List<QuestionsReport> findByCid(Long id) {
        QueryWrapper<QuestionsReport> wrapper = new QueryWrapper();
        wrapper.eq("cid", id);
        return reportMapper.selectList(wrapper);
    }

    @Override
    public QuestionsReport findByCidAndUserId(Long cid, Long userId) {
        QueryWrapper<QuestionsReport> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", cid);
        wrapper.eq("user_id", userId);
        return reportMapper.selectOne(wrapper);
    }

    @Override
    public Long saveReport(QuestionsReport questionsReport) {
        reportMapper.insert(questionsReport);
        return questionsReport.getId();
    }

    @Override
    public Long updateReport(QuestionsReport questionsReport) {
        reportMapper.updateById(questionsReport);
        return questionsReport.getId();
    }

    @Override
    public QuestionsReport findById(Long reportId) {
        return reportMapper.selectById(reportId);
    }

    @Override
    public List<QuestionsReport> findAll() {
        return reportMapper.selectList(null);
    }
}
